﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using common;
using DTC.BLL;
using Common;
using System.Threading;

namespace DTC
{
    public partial class QueryExcel :UIBase
    {
        bool busy=false;
        public QueryExcel()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }
        public Action actRefresh = null;     
        private void button1_Click(object sender, EventArgs e)
        {
            Thread trd = new Thread(SaveExcel);
            trd.Start();
        }

        private void SaveExcel()
        {
            try
            {
                busy=true;
                label4.Text = "查询中...";
                string Begin = dp1.Value.ToString("yyyy-MM-dd ") + tp1.Value.ToString("HH:mm:ss");
                string End = dp2.Value.ToString("yyyy-MM-dd ") + tp2.Value.ToString("HH:mm:ss");
                DataTable dt = DTC.BLL.Test_DatasManager.GetTestDataTable(Begin, End);
                label4.Text = "保存中...";
                common.recordsaver.saveexcel(dt, Begin, End);
                if (actRefresh != null)
                    actRefresh();
                label4.Text = "保存完成";
                ShowTopic("数据导出完成(文件保存在D:\\ExcelReport)");
                busy = false;
                this.Close();
            }
            catch (Exception ex)
            {
                busy = false;
                Common.LogHelper.WriteLog(typeof(QueryExcel), ex);
                label4.Text = "查询出现异常";
            }
        }

        private void QueryExcel_Load(object sender, EventArgs e)
        {
            dp2.Value = DateTime.Now;
            tp2.Value = DateTime.Now;
            dp1.Value = DateTime.Now.AddDays(-1);
            tp1.Value = DateTime.Now.AddDays(-1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
                this.Close();
        }

        private void QueryExcel_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (busy)
            {
                ShowTopic("忙碌中,请等待...");
                e.Cancel = true;
                return;
            }
        }
    }
}
